﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PanGu;
using PanGu.Dict;

namespace WordSegments
{
    public class OPSegmentUtils
    {
        private PanGu.Match.MatchOptions _options;
        private PanGu.Match.MatchParameter _parameters;

        static OPSegmentUtils instance;

        public OPSegmentUtils sharedInstance
        {
            get
            {
                if(instance == null)
                {
                    instance = new OPSegmentUtils();
                }
                return instance;
            }
        }
        public OPSegmentUtils()
        {
            PanGu.Segment.Init();
            _options = PanGu.Setting.PanGuSettings.Config.MatchOptions;

            _options.FrequencyFirst = true;
            _options.FilterStopWords = true;
            _options.ChineseNameIdentify = true;
            _options.MultiDimensionality = true;
            _options.EnglishMultiDimensionality = false;
            _options.ForceSingleWord = false;
            _options.TraditionalChineseEnabled = false;
            _options.OutputSimplifiedTraditional = false;
            _options.UnknownWordIdentify = false;
            _options.FilterEnglish = false;
            _options.FilterNumeric = true;
            _options.IgnoreCapital = true;
            _options.EnglishSegment = false;
            _options.SynonymOutput = false;
            _options.WildcardOutput = false;
            _options.WildcardSegment = false;
            _options.CustomRule = false;

            _parameters.Redundancy = 0;
            _parameters.FilterEnglishLength = 0;
            _parameters.FilterNumericLength = 0;
        }

        public ICollection<WordInfo> doSegments(string stringToSeg)
        {
            Segment segment = new Segment();
            return segment.DoSegment(stringToSeg, _options, _parameters);
        }

        public string strFenCiIndexer(string stringToIndex)
        {
            ICollection<WordInfo> words = doSegments(stringToIndex);
            var indexer = "";
            words.ToList().ForEach(w =>
                {
                    if(w != null)
                    {
                        indexer += w.Word + " ";
                    }
                });
            indexer = indexer.TrimEnd(' ');
            return indexer;
        }
    }
}